package com.evil.application.strategy.reporting;

import com.evil.application.enums.reporting.ReportingTypeEnum;
import com.evil.application.pojo.dto.reporting.ApplicationReportingDTO;
import com.evil.application.pojo.dto.reporting.data.ReportingDataRespDTO;
import com.evil.application.pojo.dto.reporting.data.ReportingTreeDataDTO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.search.aggregations.Aggregations;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 报表类型-饼状图
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@Slf4j
@Service
@AllArgsConstructor
public class ReportingDataTreeChartImpl implements ReportingDataChart {

    @Override
    public ReportingTypeEnum type() {
        return ReportingTypeEnum.TREE_CHART;
    }

    /**
     * 统计
     *
     * @param reportingDTO reportingDTO
     * @param aggregations aggregations
     * @return ReportingDataRespDTO
     */
    @Override
    public ReportingDataRespDTO calculation(ApplicationReportingDTO reportingDTO, Aggregations aggregations) {
        ReportingDataRespDTO reportingDataRespDTO = new ReportingDataRespDTO(reportingDTO.getReportingKey());

        SummaryTree summaryTree = this.summaryTree(reportingDTO, aggregations);
        reportingDataRespDTO.setTreeValues(this.recursion(summaryTree));
        return reportingDataRespDTO;
    }

    private List<ReportingTreeDataDTO> recursion(SummaryTree summaryTree) {
        List<ReportingTreeDataDTO> treeValues = new ArrayList<>();
        summaryTree.getChildren().forEach(e -> {
            ReportingTreeDataDTO tree = new ReportingTreeDataDTO(e.getSummary().getTitle(), e.getSummary().getValue());
            tree.setChildren(this.recursion(e));
            treeValues.add(tree);
        });
        return treeValues;
    }
}
